package com.example.android2019.ch3;

import android.content.Intent;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android2019.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Ch3FileOuterActivity extends AppCompatActivity {
    Button btn1, btn2;
    TextView tv;
    String fileName = "hello2.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch3_file_outer);
         /*外部保存*/
        setContentView(R.layout.activity_ch3_file_outer);
        btn1 = findViewById(R.id.ch3_file_outer_btn_write);
        btn2 = findViewById(R.id.ch3_file_outer_btn_read);
        tv = findViewById(R.id.ch3_file_outer_tv);

        /*动态申请权限*/
        String[] p = {"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(Ch3FileOuterActivity.this, p, 1);

        /*按钮点击事件及绑定*/
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ch3_file_outer_btn_write:
                        write();
                        break;
                    case R.id.ch3_file_outer_btn_read:
                        read();
                        break;
                }
            }
        };
        btn1.setOnClickListener(listener);
        btn2.setOnClickListener(listener);

        /*获取传递的数据*/
        Intent intent = this.getIntent();
        String a = intent.getStringExtra("test");
        Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
    }

    private void read() {
        String device = Environment.getExternalStorageState();
        if (device.equals(Environment.MEDIA_MOUNTED)) {
            File path = Environment.getExternalStorageDirectory();
            File file = new File(path, "h2.txt");
            FileInputStream in = null;
            BufferedReader br = null;
            try {
                in = new FileInputStream(file);
                br = new BufferedReader(new InputStreamReader(in));
                String line = null;
                String s = "";
                while ((line = br.readLine()) != null) {
                    s += line + "\n";
                }
                tv.setText(s);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (br != null) {
                    try {
                        br.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private void write() {
        String message = "Android\nIOS";
        /*获取外部设备的状态*/
        String device = Environment.getExternalStorageState();
        /*获取外部存储设备的路径*/
        File path = Environment.getExternalStorageDirectory();
        /*file存储的路径*/
        File file = new File(path, "h2.txt");
        /*通过IO流的方式将massage*/
        FileOutputStream out = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(file);
            bw = new BufferedWriter(new OutputStreamWriter(out));
            bw.write(message);
            bw.flush();
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
